/*jslint vars:true plusplus:true*/

//=========================================================
// Interactive Digital Media Diploma
// CUFDIG404A_R1: Introduction to Scripting
// Assignment 3
// Author: Ben
// File: entity.js
// Description: Simple sprite/entity system
//=========================================================

// sprite object to store sprite information
function Sprite(src, onLoad, cellWidth, cellHeight) {
    "use strict";
    var self = this;
    this.image = new Image();
    this.offsetX = 0;
    this.offsetY = 0;
        
    this.image.onload = function () {
        if (onLoad !== undefined) {
            onLoad(self);
        }
        
        // if no cell height is defined then use image width and height
        if (cellHeight === undefined){
            cellHeight = self.image.height;
        }

        if (cellWidth === undefined){
            cellWidth = self.image.width;
        }
    };
    
    this.getXCells = function(){
        return Math.floor(this.image.width / cellWidth);
    }
    
    this.getYCells = function(){
        return Math.floor(this.image.height / cellHeight);
    }

    this.draw = function(context, x, y, col, row){
        var sheetX = col * cellWidth;
        var sheetY = row * cellHeight;
        context.drawImage(self.image,  sheetX, sheetY, cellWidth, cellHeight, x + self.offsetX, y + self.offsetY, cellWidth, cellHeight );

    }
    this.image.src = src;


}

// base game entity. Contains a sprite and a position
function Entity(sprite, initX, initY, visible) {
    "use strict";
    var self = this;
    self.x = initX;
    self.y = initY;
    self.isVisible = true;
    self.spriteXCell = 0;
    self.spriteYCell = 0;
    
    if (visible !== undefined) {
        self.isVisible = visible;
    }
    
    // offset of the sprite from the entity position
    self.draw = function (context) {
        if (self.isVisible){
            sprite.draw(context, self.x, self.y, self.spriteXCell, self.spriteYCell);
        }
    };
    
    self.incSprite = function(){
        self.spriteXCell++;
        if (self.spriteXCell >= sprite.getXCells()){
            self.spriteXCell = 0;
            self.spriteYCell++;
            if (self.spriteYCell >= sprite.getYCells()){
                self.spriteYCell = 0;
            }
        }
    }
    
    window.game.entityManager.addEntity(self);
}


function EntityManager() {
    "use strict";
    
    var entities = [];
    
    
    this.addEntity = function (entity) {
        entities.push(entity);
    };
    
    
    this.removeEntity = function (entity) {
        var idx = entities.indexOf(entity);
        entities.splice(idx, 1);
    };
    
    
    this.draw = function (context) {
        var idx;
        for (idx = 0; idx < entities.length; idx++) {
            if (entities[idx].isVisible) {
                entities[idx].draw(context);
            }
        }
    };
}

window.game.entityManager = new EntityManager();
window.game.addSubsystem(window.game.entityManager);